package yanchun.vip.spring.ioc.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import yanchun.vip.spring.ioc.model.po.User;

/**
 * BeanPostProcessor：bean后置处理器接口
 * @author SuYanchun
 * @since 2025-02-10 17:24
 */
@Component
public class BeanPostProcessorImpl implements BeanPostProcessor {

    /**
     * 初始化之前的后置处理器
     * @param bean 组件
     * @param beanName 组件名称
     * @return 组件
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {


        if (bean instanceof User user) {
            System.out.println("【BeanPostProcessor】—— postProcessBeforeInitialization方法--"+user);
        }

        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }

    /**
     * 初始化之后的后置处理器
     * @param bean 组件
     * @param beanName 组件名称
     * @return 组件
     * @throws BeansException
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof User user) {
            System.out.println("【BeanPostProcessor】—— postProcessAfterInitialization方法--"+user);
        }
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }

}
